package com.ygsj.Utils;




import android.support.annotation.NonNull;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/3.
 */
public class OkhttpUtils {
    public static final int NETWORK_ERROR_CODE = 1;
    private static OkHttpClient client;
    private static Request request;
    private static Call call;
    private static int curProgress;// 进度条的当前刻度值

    public static OkHttpClient getClient() {
        client = Singleton.getOkhttpInstance();
        return client;
    }
    //创建OkHttpClient对象
    public static void get_getJson(String url, Callback callback) {
        //创建OkHttpClient对象
        getClient().cache();
        //创建Request对象
        request = new Request.Builder()
                .url(url)
                .build();
        //创建执行任务的call对象
        call = getClient().newCall(request);
        //执行异步任务
        call.enqueue(callback);

    }

    //post请求
    public static void post_GetJson(String url, Callback callback,FormBody.Builder builder) {
//        FormBody.Builder builder = new FormBody.Builder();
//        //数据不对需要重写；
//        builder.add("code", "gehj");
//        builder.add("password", "20");
       // builder.add("serialIds", "2143,3404");
       // builder.add("v", "4.0.0");*
        request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();
        call = getClient().newCall(request);
        call.enqueue(callback);

    }



    public static void cancelAllCall(){
        getClient().dispatcher().cancelAll();

    }
}
